<?php
	function check_dir($pid, $OJ_DATA){
		$path = $OJ_DATA . '/' . $pid;
		$ret = false;
		$fp = fopen($path."/testifwriteable.tst","w");
		$ret = !($fp === false);
		fclose($fp);
		unlink($path."/testifwriteable.tst");
		return $ret;
	}

	function get_test($pid, $OJ_DATA){
		$files = array();
		$path = $OJ_DATA . '/' . $pid;
		$mydir = dir($path);
		while($file = $mydir->read()){
			if (!is_file($path . '/' . $file)) continue;
			if (strpos($file, ".in") != strlen($file) - 3) continue;
			$file = substr($file, 0, strlen($file) - 3);
			array_push($files, $file);
		}
		return $files;
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel=stylesheet href='../include/hoj.css' type='text/css'>
<title>Edit Problem Data</title>
</head>
<body>
<center>
<?php require_once("../include/db_info.inc.php");
$id = (int)$_GET['id'];

if (!(isset($_SESSION['administrator']))){
	echo "<a href='../loginpage.php'>Please Login First!</a>";
	exit(1);
}

if (!check_dir($id, $OJ_DATA)){
	echo "Problem Error!";
	exit(1);
}
?>
<p align="center"><font color="#333399" size="4">Welcome To Administrator's Page of Judge Online of ACM ICPC, <?php echo $OJ_NAME?>.</font></p>
<form action="problem_data_edit_page2.php" method="get">
<table width="600px" border="1">
<tr>
<td>File List</td>
<td width="100px">Modify</td>
</tr>
<?php
	$files = get_test($id, $OJ_DATA);
	foreach($files as $value){
		echo "<tr><td>";
		echo $value;
		echo "</td><td>";
		echo "<a href='problem_data_edit_page2.php?id={$id}&file={$value}'>Edit</a></td></tr>";
	}	
?>
<tr><td colspan="2">Filename:<input type="text" name="file" id="file" />&nbsp;<input type="submit" value="add" /><input type="hidden" name="id" id="id" value="<?php echo $id;?>" /></td></tr>
</table>
</form>
</center>
<?php
	require_once "../oj-footer.php";
?>
